package com.diamond.facade;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import com.diamond.bo.QueryUserBO;
import com.diamond.bo.ReturnBO;
import com.diamond.bo.SalesBO;
import com.diamond.bo.UserBO;
import com.diamond.entity.User;
import com.diamond.po.UserPO;
import com.diamond.service.IUserService;
import com.diamond.util.DateUtils;
import com.diamond.util.MD5;
import com.diamond.util.StringUtil;

public class UserFacade extends CommonFacade<UserBO> {
	
	public IUserService userService;

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	public void addOrUpdateUser(UserBO userBo) throws Exception{
		UserPO userPo = new UserPO();
		PropertyUtils.copyProperties(userPo, userBo);
		//验证改用户是新增还是修改，如果是新增，则设置默认密码，如果是修改，则不改变密码
		UserBO resultUserBo = this.findUserByUserId(String.valueOf(userPo.getUserId()));
		if (StringUtil.isEmpty(resultUserBo)) {
			if ("E".equals(userPo.getUserType())) {
				userPo.setPassword(MD5.getDefaultPwdE());
				userPo.setCmpName("管理员公司");
			} else if ("B".equals(userPo.getUserType())) {
				userPo.setPassword(MD5.encode(userBo.getContactTel()));
			} else if ("C".equals(userPo.getUserType())) {
				userPo.setPassword(MD5.encode(userBo.getContactTel()));
			} else if ("D".equals(userPo.getUserType())) {
				userPo.setPassword(MD5.getDefaultPwdD());
			} else {
				throw new Exception();
			}
		} else {
			userPo.setPassword(resultUserBo.getPassword());
		}
		userPo.setRegisterDate(DateUtils.getStringDate());
		userService.addOrUpdateUser(userPo);
	}
	
	public void updateUser(UserBO userBo){
		if(userBo==null)return;
		UserPO userPo = new UserPO();
		try {
			PropertyUtils.copyProperties(userPo, userBo);
			userService.addOrUpdateUser(userPo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public Boolean updateBackUserPW(Long userId, String oldPassword, String newPassword) {
		Boolean result = false;
		try {
			result = userService.updateBackUserPW(userId, oldPassword, newPassword);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public ReturnBO<UserBO> queryUser(QueryUserBO queryUserBo) throws Exception {
		ReturnBO<UserBO> returnBo = userService.queryUser(queryUserBo);
		return returnBo;
	}

	public UserBO findUserByUserId(String userId) throws Exception {
		UserBO userBo = userService.findUserByUserId(userId);
		return userBo;
	}

	public Boolean deleteUser(String[] userIds) throws Exception {
		return userService.deleteUser(userIds);		
	}

	public Boolean checkUserName(String userName) throws Exception {
		return userService.checkUserName(userName);
	}

	public List<UserBO> queryUserByUserType(String[] userTypes) throws Exception {
		return userService.queryUserByUserType(userTypes);
	}

	public List<UserBO> backLogin(UserBO userBo) throws Exception {
		return userService.backLogin(userBo);
	}

	public User login(UserBO userBo) {
		return userService.login(userBo);
	}

	public User loginCenter(UserBO userBo) {
		return userService.loginCenter(userBo);
	}

	public void updateSonPass(Long userId,String pass) {
		userService.updateSonPass(userId,pass);
	}

	public List getSales(Long userId,SalesBO salesBO) {
		return userService.getSales(userId,salesBO);
	}

	public List<UserBO> searchSubUser(Long userId) throws Exception{
		return userService.searchSubUser(userId);
	}

	public void updateSonPassByUserId(Long subUserId, String surepass) {
		userService.updateSonPassByUserId(subUserId,surepass);
	}
}
